/**
 * 
 */
package com.estock.crawler.utils;

import java.io.File;
import java.util.Calendar;

import com.estock.common.ESConfig;

/**
 * @author richard
 *
 */
public class ESCrawlerUtil {
	
	public static final String DOMIN_10jqka = "http://basic.10jqka.com.cn/";
	
	public static String getRootDataDir() {
		return getLatestCrawler4jDataDir(true) + "root/";
	}
	
	public static String get10jqkaRawDataDir() {
		return getLatestCrawler4jDataDir(false) + "10jqka/";
	}
	
	public static String get10jqkaCsvDir() {
		return getLatestCrawler4jDataDir(false) + "csv/";
	}
	
	public static void deleteFile(String filename) {
		File file = new File(filename);
		if (file.isFile()) {
			file.delete();
		}
	}
	
	private static String getLatestCrawler4jDataDir(boolean getDirForCurrentMonth) {
		String dataFileDir = ESConfig.getInstance().getDataFileDir() + "crawler4j/";
		if (getDirForCurrentMonth) {
			Calendar cal = Calendar.getInstance();
			return dataFileDir + String.format("%s%02d.csv", cal.get(Calendar.YEAR), cal.get(Calendar.MONTH) + 1) + "/";
		}
		
    	File direcory = new File(dataFileDir); 
		File[] files = direcory.listFiles();
		String maxFileName = files[0].getName();
		for (File file : files) {
			if (file.getName().compareTo(maxFileName) == 1) {
				maxFileName = file.getName();
			}
		}
		
		return dataFileDir + maxFileName + "/";
	}
	
	///// Testing method
	public static void main(String[] args) {
		System.out.println(getLatestCrawler4jDataDir(false));
	}

}
